package com.simple.mq.core.network.core;

import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 项目: simple-mq
 * <p>
 * 功能描述: 实际的消息队列
 *
 * @author: WuChengXing
 * @create: 2023-09-04 14:26
 **/
public class MessageQueue extends LinkedBlockingQueue<Object> {

    /**
     * 每个queue的bindingKey，同一个包下才能够访问
     */
    private final String bindingKey;

    /**
     * 为每个queue起一个具有分辨性的名字，同一个包下才能够访问
     */
    private final String name;

    /**
     * 用于处理没有消费者消费的情况
     */
    public final CountDownLatch latch;

    /**
     * 在当前queue上工作的所有的worker线程，只允许同包访问
     */
    final ArrayList<Thread> workers = new ArrayList<>();

    public String getBindingKey() {
        return bindingKey;
    }

    public String getName() {
        return name;
    }

    public MessageQueue(String bindingKey, String name) {
        super();
        this.bindingKey = bindingKey;
        this.name = name;
        latch = new CountDownLatch(1);
    }

    @Override
    public String toString() {
        return "MessageQueue{" +
                "bindingKey='" + bindingKey + '\'' +
                ", name='" + name + '\'' +
                ", elements=" + super.toString() +
                '}';
    }
}
